#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2014 OpenWrt.org

START=120
STOP=120
#PROCD_DEBUG=1
USE_PROCD=1
#SERVICE_WRITE_PID=1
PROG=/usr/bin/socat
NAME=sub-ghz


validate_section_socat()
{
	uci_validate_section $NAME $NAME "${1}" \
	,'enabled:bool:1' \


       return $?  
}

app_instance()
{
	local enabled
	local server_addr server_port
	local local_port
	local MaxWaitTime
	local socat_log_enable
	local ReConnectTime
	local protocol
	local AT_Serial
	local databit
	local stopbit
	local baudrate
	local parity_check
	local power
	local def_channel

	echo "app_instance $1 $2"

	config_get enabled  $1 enabled

#	validate_section_socat "${1}" || {
#		echo "validation failed"
#		return 1
#	}


	echo "${NAME} app_instance"



	echo "enabled :${enabled}"

	[ "${enabled}" = "0" ] && return 1

#	echo "READ_AT_SERIAL"	

#	[ "${AT_Data_Serial}" = "" ] && return 1


#	echo "AT_Serial:${AT_Data_Serial}"


	config_get  power         $1  power
	config_get  def_channel   $1  def_channel
	config_get  AT_Serial  $1 AT_Serial
	config_get protocol       $1	protocol
	config_get     server_port  $1 server_port
	config_get     server_addr  $1 server_addr
	config_get     local_port   $1 local_port
	config_get     server_port  $1 server_port
	config_get     ReConnectTime $1 ReConnectTime
	config_get     MaxWaitTime $1 MaxWaitTime
	config_get     log_enable $1 log_enable


	echo "${NAME} instance."
	echo "${NAME} instance." >> /var/log/${NAME}.log

	procd_open_instance
	procd_set_param command "$PROG"
	
#       option SocatOptions '-d -d  -lf /var/log/socat.log tcp4-connect:192.168.1.1:8000 tcp4-connect:139.196.83.26:8010'
	
#	SocatOptions="-d -d -T ${MaxWaitTime} -lf /var/log/socat.log tcp4-connect:${lan_ip}:${local_port} tcp4-connect:${server_addr}:${server_port}"

#socat -d -d /dev/ttyS1,raw,nonblock,ignoreeof,B115200,CS7,PARENB,PARODD,cr,echo=0 TCP4-LISTEN:5555,reuseaddr
	SocatOptions=""
	arg_net=""

	if [ ! -c $AT_Serial ]; then
		echo "$AT_Serial AT_Serial Not found"  >> /var/log/socat_ser2net.log
		return 1
	else
		echo "$AT_Serial AT_Serial is existed"  >> /var/log/socat_ser2net.log
		#echo -e  "AT+POWER=${power}\r\n" > $$AT_CMD_Serial
		#echo -e  "AT+CHANNEL=${def_channel}\r\n" > $$AT_CMD_Serial
	fi                                       
                       
                     
	baudrate="B115200"
	arg_check=""
	arg_stopbit=""
	arg_net=""
	databit="CS8"
	arg_stopbit=""


	if [[ "${protocol}" = "tcp-listen" || "${protocol}" = "udp-listen" ]]
	then
		arg_net="${protocol}:${server_port}"
	else
		arg_net="${protocol}:${server_addr}:${server_port}"
	fi

	echo "arg_net:$arg_net"

	if [ "${log_enable}" = "1" ]
	then
		echo "record log" >> /var/log/${NAME} .log
#		SocatOptions="-d -d -T ${MaxWaitTime} -lf /var/log/socat_ser2net.log tcp4-connect:${local_ip}:${local_port} tcp4-connect:${server_addr}:${server_port}"
		SocatOptions="-d -d -T ${MaxWaitTime} -lf /var/log/${NAME}.log ${AT_Serial},raw,nonblock,ignoreeof,cr,echo=0 ${arg_net}"
		echo "SocatOptions:${SocatOptions}"
	else
		SocatOptions="-d -d -T ${MaxWaitTime} ${AT_Serial},raw,nonblock,ignoreeof,cr,echo=0 ${arg_net}"
		echo "SocatOptions:${SocatOptions}"	
	fi

	procd_append_param command ${SocatOptions}
        
	echo "respawn ${respawn_threshold:-60} ${respawn_timeout:-${ReConnectTime}} ${respawn_retry:-4294967295} " >> /var/log/${NAME}.log	
	procd_set_param respawn ${respawn_threshold:-60} ${respawn_timeout:-${ReConnectTime}} ${respawn_retry:-4294967295}
#	SERVICE_PID_FILE=/var/run/$NAME.pid
	procd_set_param pidfile /var/run/$NAME.pid
#	procd_set_param file /etc/config/${NAME}
	procd_close_instance

}
service_triggers() {
        procd_add_reload_trigger "${NAME}"
}

start_service () {

	#SET THE MODULE 
	/usr/sbin/sub-ghz-at-cmd.lua    
	echo "start_service ${NAME} $`date` "                                  
	config_load "${NAME}"

	#NOTES: THIS IS A INTERFERFACE
	config_foreach app_instance interface  rfconfig
}


stop () {
	echo "stop ${NAME} $date "                                  
	echo "${NAME} stop. $date" >> /var/log/${NAME}.log
	service_stop $PROG
}



start () {
	echo "start ${NAME} $date "                                  
	echo "${NAME} start. $date" >> /var/log/${NAME}.log
	start_service
}


restart () {
        	
	echo "restart ${NAME} $date "                                  
	echo "restart ${NAME} $date " >> /var/log/${NAME}.log                                     
	stop 
	start
}

#reload (){
#	service_reload
#	reload_service
#	stop
#	start_service
#}

#reload_service()
#{
#        echo "reload service ${NAME} $date " >> /var/log/${NAME}.log
#        stop
#	start_service
#}

reload_service() {
	local rv=0
	ubus call "${NAME}" reload || rv=1
	restart
	return $rv
}

